joined_vacomparison <- joined_vacomparison %>%
mutate(
dem_dif = round_half_up((pct_mcauliffe - biden_pct), 2),
rep_dif = round_half_up((pct_youngkin - trump_pct), 2)
) Virginia Election Project
Comparing Virgnia Gov vs. Prez
#joined_vacomparison <- joined_vacomparison %>%
#arrange(dem_dif)joined_vacomparison <- joined_vacomparison %>%
relocate(dem_dif, rep_dif, .after = locality)
head(joined_vacomparison)# A tibble: 6 × 11
locality dem_dif rep_dif biden trump biden…¹ trump…² young…³ mcaul…⁴ pct_y…⁵
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl>
1 ACCOMACK … -6.31 7.01 7578 9172 44.7 54.1 7878 4948 61.1
2 ALBEMARLE… -3.63 5.03 42466 20804 65.7 32.2 19141 31919 37.2
3 ALEXANDRI… -5.08 6.39 66240 14544 80.3 17.6 14013 43866 24.0
4 ALLEGHANY… -2.37 3.09 2243 5859 27.3 71.4 4530 1518 74.5
5 AMELIA CO… -5.13 5.9 2411 5390 30.6 68.3 4720 1617 74.2
6 AMHERST C… -4.92 6.07 5672 11041 33.4 64.9 9731 3897 71
# … with 1 more variable: pct_mcauliffe <dbl>, and abbreviated variable names
# ¹biden_pct, ²trump_pct, ³youngkin, ⁴mcauliffe, ⁵pct_youngkin
tail(joined_vacomparison)# A tibble: 6 × 11
locality dem_dif rep_dif biden trump biden…¹ trump…² young…³ mcaul…⁴ pct_y…⁵
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl>
1 WESTMOREL… -6.32 7.01 4501 5318 45.3 53.5 4614 2971 60.6
2 WILLIAMSB… -5 6.02 4790 1963 69.6 28.5 1703 3185 34.5
3 WINCHESTE… -4.06 5.56 6610 5221 54.6 43.1 4137 4294 48.7
4 WISE COUN… -3.17 3.45 3110 13366 18.7 80.4 9691 1796 83.9
5 WYTHE COU… -3.18 3.93 3143 11733 20.8 77.8 9458 2043 81.8
6 YORK COUN… -4.74 6.4 17683 20241 45.6 52.2 17485 12190 58.6
# … with 1 more variable: pct_mcauliffe <dbl>, and abbreviated variable names
# ¹biden_pct, ²trump_pct, ³youngkin, ⁴mcauliffe, ⁵pct_youngkin
# uncomment to run, then recomment it out so you don't run it every time
#census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE)#chose variables we want
myvars <- c(totalpop = "B01003_001",
medincome = "B19013_001",
medage = "B01002_001"
)#pull for VA counties
va_counties_withgeo <- get_acs(geography = "county",
variables = c(myvars),
state = "VA",
output = "wide",
geometry = TRUE)Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 16%
|
|============== | 20%
|
|============== | 21%
|
|================ | 23%
|
|================= | 25%
|
|==================== | 28%
|
|==================== | 29%
|
|======================= | 33%
|
|======================== | 34%
|
|========================= | 36%
|
|========================== | 36%
|
|=========================== | 38%
|
|============================ | 40%
|
|============================= | 41%
|
|============================== | 43%
|
|=============================== | 45%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|====================================== | 54%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|========================================= | 58%
|
|========================================== | 60%
|
|============================================ | 62%
|
|============================================= | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|================================================= | 70%
|
|================================================== | 71%
|
|==================================================== | 75%
|
|======================================================= | 78%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================== | 89%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|======================================================================| 100%
va_counties_withgeoSimple feature collection with 133 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE medincomeM
1 51105 Lee County, Virginia 22482 NA 37574 4430
2 51163 Rockbridge County, Virginia 22663 NA 57828 3580
3 51001 Accomack County, Virginia 33388 NA 50601 5803
4 51099 King George County, Virginia 26597 NA 101599 7080
5 51185 Tazewell County, Virginia 40615 NA 42937 3086
6 51029 Buckingham County, Virginia 16832 NA 49841 5835
7 51047 Culpeper County, Virginia 52021 NA 85274 5034
8 51179 Stafford County, Virginia 154204 NA 119818 3302
9 51085 Hanover County, Virginia 109232 NA 96911 2936
10 51009 Amherst County, Virginia 31381 NA 60876 3561
medageE medageM geometry
1 45.6 0.7 MULTIPOLYGON (((-83.67461 3...
2 49.2 0.6 MULTIPOLYGON (((-79.68695 3...
3 47.1 0.6 MULTIPOLYGON (((-75.74241 3...
4 38.3 0.4 MULTIPOLYGON (((-77.34744 3...
5 45.1 0.3 MULTIPOLYGON (((-81.90089 3...
6 43.0 0.8 MULTIPOLYGON (((-78.83284 3...
7 39.0 0.6 MULTIPOLYGON (((-78.22926 3...
8 35.9 0.2 MULTIPOLYGON (((-77.63422 3...
9 42.1 0.4 MULTIPOLYGON (((-77.79681 3...
10 44.9 0.5 MULTIPOLYGON (((-79.43695 3...
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
variables = c(myvars),
output = "wide",
geometry = TRUE)Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
all_counties_withgeoSimple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE
1 20161 Riley County, Kansas 72602 NA 53296
2 19159 Ringgold County, Iowa 4739 NA 57700
3 30009 Carbon County, Montana 10488 NA 63178
4 16007 Bear Lake County, Idaho 6327 NA 60337
5 55011 Buffalo County, Wisconsin 13314 NA 61167
6 31185 York County, Nebraska 14164 NA 66337
7 08037 Eagle County, Colorado 55693 NA 91338
8 42129 Westmoreland County, Pennsylvania 355107 NA 64708
9 40079 Le Flore County, Oklahoma 48436 NA 43049
10 48053 Burnet County, Texas 48424 NA 65363
medincomeM medageE medageM geometry
1 2489 25.5 0.1 MULTIPOLYGON (((-96.96095 3...
2 5058 44.3 1.0 MULTIPOLYGON (((-94.47167 4...
3 4261 50.7 0.9 MULTIPOLYGON (((-109.7987 4...
4 7039 38.9 1.1 MULTIPOLYGON (((-111.6345 4...
5 2352 46.5 0.5 MULTIPOLYGON (((-92.08384 4...
6 4128 39.5 1.2 MULTIPOLYGON (((-97.82629 4...
7 4058 37.8 0.8 MULTIPOLYGON (((-107.1137 3...
8 1350 47.1 0.2 MULTIPOLYGON (((-79.90487 4...
9 1869 38.8 0.3 MULTIPOLYGON (((-95.05996 3...
10 4694 44.7 0.3 MULTIPOLYGON (((-98.45924 3...
#remove MOE columns - they all end with "M"
va_counties_withgeo <- va_counties_withgeo %>%
select(-ends_with("M"))
va_counties_withgeoSimple feature collection with 133 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE medincomeE medageE
1 51105 Lee County, Virginia 22482 37574 45.6
2 51163 Rockbridge County, Virginia 22663 57828 49.2
3 51001 Accomack County, Virginia 33388 50601 47.1
4 51099 King George County, Virginia 26597 101599 38.3
5 51185 Tazewell County, Virginia 40615 42937 45.1
6 51029 Buckingham County, Virginia 16832 49841 43.0
7 51047 Culpeper County, Virginia 52021 85274 39.0
8 51179 Stafford County, Virginia 154204 119818 35.9
9 51085 Hanover County, Virginia 109232 96911 42.1
10 51009 Amherst County, Virginia 31381 60876 44.9
geometry
1 MULTIPOLYGON (((-83.67461 3...
2 MULTIPOLYGON (((-79.68695 3...
3 MULTIPOLYGON (((-75.74241 3...
4 MULTIPOLYGON (((-77.34744 3...
5 MULTIPOLYGON (((-81.90089 3...
6 MULTIPOLYGON (((-78.83284 3...
7 MULTIPOLYGON (((-78.22926 3...
8 MULTIPOLYGON (((-77.63422 3...
9 MULTIPOLYGON (((-77.79681 3...
10 MULTIPOLYGON (((-79.43695 3...
#remove that trailing "E"
colnames(va_counties_withgeo) <- sub("E$", "", colnames(va_counties_withgeo)) # $ means end of string only
va_counties_withgeoSimple feature collection with 133 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAM totalpop medincome medage
1 51105 Lee County, Virginia 22482 37574 45.6
2 51163 Rockbridge County, Virginia 22663 57828 49.2
3 51001 Accomack County, Virginia 33388 50601 47.1
4 51099 King George County, Virginia 26597 101599 38.3
5 51185 Tazewell County, Virginia 40615 42937 45.1
6 51029 Buckingham County, Virginia 16832 49841 43.0
7 51047 Culpeper County, Virginia 52021 85274 39.0
8 51179 Stafford County, Virginia 154204 119818 35.9
9 51085 Hanover County, Virginia 109232 96911 42.1
10 51009 Amherst County, Virginia 31381 60876 44.9
geometry
1 MULTIPOLYGON (((-83.67461 3...
2 MULTIPOLYGON (((-79.68695 3...
3 MULTIPOLYGON (((-75.74241 3...
4 MULTIPOLYGON (((-77.34744 3...
5 MULTIPOLYGON (((-81.90089 3...
6 MULTIPOLYGON (((-78.83284 3...
7 MULTIPOLYGON (((-78.22926 3...
8 MULTIPOLYGON (((-77.63422 3...
9 MULTIPOLYGON (((-77.79681 3...
10 MULTIPOLYGON (((-79.43695 3...
va_counties_withgeo <- va_counties_withgeo %>%
mutate(NAM = str_to_upper(NAM))va_counties_withgeo <- va_counties_withgeo %>%
rename("locality" = "NAM")joined_vacomparison <- joined_vacomparison %>%
mutate(locality = paste(locality, ", VIRGINIA", sep = ""))va_counties_data <- full_join(va_counties_withgeo, joined_vacomparison)Joining with `by = join_by(locality)`
Mapping Virginia counties with mapview
mapview(va_counties_data, zcol = "dem_dif")To turn off legends, hover text, popups
mapview(va_counties_data, zcol = "dem_dif",
col.regions = RColorBrewer::brewer.pal(9, "Blues"),
alpha.regions = 1,
legend = FALSE,
label = FALSE,
popup = FALSE)Warning: Found less unique colors (9) than unique zcol values (116)!
Interpolating color vector to match number of zcol values.
Customize labels
mylabel <- glue::glue("{va_counties_data$locality} {va_counties_data$dem_dif}")
mapview(va_counties_data, zcol = "dem_dif",
col.regions = RColorBrewer::brewer.pal(9, "Blues"),
alpha.regions = 1,
label = mylabel)Warning: Found less unique colors (9) than unique zcol values (116)!
Interpolating color vector to match number of zcol values.
Customize popups
mypopup <- glue::glue("<strong>{va_counties_data$locality}</strong><br />
Total Population: {va_counties_data$totalpop}<br />
Difference in D Elections: {va_counties_data$dem_dif}<br />
Median Income: ${va_counties_data$medincome}<br />
Median Age: {va_counties_data$medage}") %>%
lapply(htmltools::HTML)
# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%
# lapply(htmltools::HTML)head(mypopup)[[1]]
<strong>LEE COUNTY, VIRGINIA</strong><br />
Total Population: 22482<br />
Difference in D Elections: -2.84<br />
Median Income: $37574<br />
Median Age: 45.6
[[2]]
<strong>ROCKBRIDGE COUNTY, VIRGINIA</strong><br />
Total Population: 22663<br />
Difference in D Elections: -2.38<br />
Median Income: $57828<br />
Median Age: 49.2
[[3]]
<strong>ACCOMACK COUNTY, VIRGINIA</strong><br />
Total Population: 33388<br />
Difference in D Elections: -6.31<br />
Median Income: $50601<br />
Median Age: 47.1
[[4]]
<strong>KING GEORGE COUNTY, VIRGINIA</strong><br />
Total Population: 26597<br />
Difference in D Elections: -6.99<br />
Median Income: $101599<br />
Median Age: 38.3
[[5]]
<strong>TAZEWELL COUNTY, VIRGINIA</strong><br />
Total Population: 40615<br />
Difference in D Elections: -2.83<br />
Median Income: $42937<br />
Median Age: 45.1
[[6]]
<strong>BUCKINGHAM COUNTY, VIRGINIA</strong><br />
Total Population: 16832<br />
Difference in D Elections: -6.6<br />
Median Income: $49841<br />
Median Age: 43
mapview(va_counties_data, zcol = "dem_dif",
col.regions = RColorBrewer::brewer.pal(9, "Blues"),
alpha.regions = 1,
popup = mypopup,
label = mylabel)Warning: Found less unique colors (9) than unique zcol values (116)!
Interpolating color vector to match number of zcol values.